import { getToken, setToken, removeToken,setTime ,removTime} from "@/utils/auth"
import { loginApi, getInfo } from '@/api/user'
import {userAddApi} from '@/api/elmentyess'
const state = {
  token: getToken(),
  // 获取用户信息
  userInfo: {},
  
}
const mutations = {
  SET_TOKEN(state, paylod) {
    state.token = paylod
    setToken(paylod)
    setTime()
  },
  REMOVE_TOKEN(state) {
    state.token=''
    removeToken()
    removTime()
  },
  // 修改用户信息
  SET_USERINFO(state, paylod) {
    state.userInfo = paylod
  },
  // 删除用户信息
  REMOVE_USERINFO(state) {
    state.userInfo={}
  }
}
const actions = {
  async GET_TOKEN(context, paylod) {
    let res = await loginApi(paylod)
    context.commit('SET_TOKEN',res.data)
  },
  // 获取用户信息
  async userInfo(context) {
    let res = await getInfo()
    // console.log(res);
    let baseres = await userAddApi(res.data.userId)
    // console.log(baseres);
    context.commit('SET_USERINFO',{...res.data,...baseres.data})
  },
  logout (context) {
    // 删除用户 token
    context.commit('REMOVE_TOKEN')
    // 删除用户信息： userInfo
    context.commit('REMOVE_USERINFO')
  }
}

export default {
  namespaced:true,
  state,
  mutations,
  actions,
}